#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys
from util.sendHttpReq import SendHttpReq
from util.getlog import get_log
from util.getAPIInfo import GetAPIInfo
from util.sendHttpReq import SendHttpReq
from configs import globalVariables
from util import writeYaml
import json
import requests
import os
'''
功能：发送api请求
入参：apiName
返回值：response body
'''

def sendAPIRequest(apiName,apiFileName, files = None, **kw): #支持改写从api_config.yml中读取出的data,files后，再发送
    APIInfo = GetAPIInfo().getAPISendInfo(apiName,apiFileName)
    sender = SendHttpReq()#实例化类

    if kw:
        data_updateKeys = list(kw.keys())
        for key in data_updateKeys:
            APIInfo['data'][key] = kw[key]
    if files:
        # 注：上传文件的request，headers中不要传Content-Type
        APIInfo['files'] = files
        resBody = sender.sendHttpReq(APIInfo['method'], APIInfo['url'], APIInfo['headers'], data = APIInfo['data'], files = APIInfo['files'])
    else:
        resBody = sender.sendHttpReq(APIInfo['method'], APIInfo['url'], APIInfo['headers'], APIInfo['data'])
    return resBody

if __name__ == "__main__":
    res1 = sendAPIRequest('getCourseInfoList','api_trainingAndExam.yml')
    res2 = sendAPIRequest('getCourseInfoList','api_trainingAndExam.yml',courseOrder=81)
    print('res', res1)
    print('res',res2)